package com.astronomy.article.service;

import com.astronomy.article.mapper.ArticleMapper;
import com.astronomy.article.pojo.Article;
import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class ArticleService {
    public static final String ARTICLE_KEY = "articel_";//redis key前缀
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IdWorker idWorker;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 全部数据
     *
     * @return
     */
    public List<Article> findAll() {
        return articleMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public Article findById(String id) {
        Article article = null;
        //先从缓存中查询
        article = (Article) redisTemplate.opsForValue().get(ARTICLE_KEY+id);
        System.out.println(article);
        if (null !=article){
            //滑动过期
            redisTemplate.expire(ARTICLE_KEY+id,20000,TimeUnit.MILLISECONDS);
            System.out.println("缓存中得到数据");
            return article;
        }
        //从数据库中查询
        article = articleMapper.selectByPrimaryKey(id);
        if (null !=article){
            redisTemplate.opsForValue().set(ARTICLE_KEY+id,article);
            redisTemplate.expire(ARTICLE_KEY+id,20000,TimeUnit.MILLISECONDS);
        }

        return article;
    }

    /**
     * 增加
     *
     * @param article
     */
    public void add(Article article) {
        article.setId(idWorker.nextId()+"");
        articleMapper.insert(article);
    }

    /**
     * 修改
     *
     * @param article
     */
    public void update(Article article) {
        //修改之前先删掉redis的key
        redisTemplate.delete(ARTICLE_KEY + article.getId());
        articleMapper.updateByPrimaryKey(article);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        //删除数据库之前先删掉缓存里的key
        redisTemplate.delete(ARTICLE_KEY + id);
        articleMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Article> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Article>) articleMapper.selectAll();
    }

    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    public List<Article> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return articleMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // ID
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andLike("id", "%" + searchMap.get("id") + "%");
            }
            // 专栏ID
            if (searchMap.get("columnid") != null && !"".equals(searchMap.get("columnid"))) {
                criteria.andLike("columnid", "%" + searchMap.get("columnid") + "%");
            }
            // 用户ID
            if (searchMap.get("userid") != null && !"".equals(searchMap.get("userid"))) {
                criteria.andLike("userid", "%" + searchMap.get("userid") + "%");
            }
            // 标题
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                criteria.andLike("title", "%" + searchMap.get("title") + "%");
            }
            // 文章正文
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content"))) {
                criteria.andLike("content", "%" + searchMap.get("content") + "%");
            }
            // 文章封面
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 是否公开
            if (searchMap.get("ispublic") != null && !"".equals(searchMap.get("ispublic"))) {
                criteria.andLike("ispublic", "%" + searchMap.get("ispublic") + "%");
            }
            // 是否置顶
            if (searchMap.get("istop") != null && !"".equals(searchMap.get("istop"))) {
                criteria.andLike("istop", "%" + searchMap.get("istop") + "%");
            }
            // 审核状态
            if (searchMap.get("state") != null && !"".equals(searchMap.get("state"))) {
                criteria.andLike("state", "%" + searchMap.get("state") + "%");
            }
            // 所属频道
            if (searchMap.get("channelid") != null && !"".equals(searchMap.get("channelid"))) {
                criteria.andLike("channelid", "%" + searchMap.get("channelid") + "%");
            }
            // URL
            if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
                criteria.andLike("url", "%" + searchMap.get("url") + "%");
            }
            // 类型
            if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
                criteria.andLike("type", "%" + searchMap.get("type") + "%");
            }

            // 浏览量
            if (searchMap.get("visits") != null) {
                criteria.andEqualTo("visits", searchMap.get("visits"));
            }
            // 点赞数
            if (searchMap.get("thumbup") != null) {
                criteria.andEqualTo("thumbup", searchMap.get("thumbup"));
            }
            // 评论数
            if (searchMap.get("comment") != null) {
                criteria.andEqualTo("comment", searchMap.get("comment"));
            }

        }
        return example;
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    public PageResult<Article> findPage(Map<String, Object> searchMap, int page, int size) {
        //创建查询条件
        Example example = createExample(searchMap);
        //启动分页
        PageHelper.startPage(page, size);
        //查询结果
        List<Article> articles = articleMapper.selectByExample(example);
        //把查询结果封装成PageInfo
        PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
        //返回结果
        return new PageResult<>(articlePageInfo.getTotal(), articlePageInfo.getList());
    }

    /**
     * 文章审核
     *
     * @param id
     */
    public void updateState(String id) {
        articleMapper.updateState(id);
    }

    /**
     * 点赞
     *
     * @param id
     */
    public void addThumbup(String id) {
        articleMapper.addThumbup(id);
    }

}
